package com.qfedu.api.service.Impl;

import com.qfedu.api.service.UserRegistService;
import com.qfedu.api.service.feign.UserCheckClient;
import com.qfedu.api.service.feign.UserSaveClient;
import com.qfedu.fmmall.beans.Users;
import com.qfedu.fmmall.utils.MD5Utils;
import com.qfedu.fmmall.vo.ResultStatus;
import com.qfedu.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;


@Service
public class UserRegistServiceImpl implements UserRegistService {

    @Autowired
    private UserCheckClient userCheckClient;

    @Autowired
    private UserSaveClient userSaveClient;

    @Override
    public ResultVo userResgit(String username, String password) {

        synchronized (this){ //让注册服务统一在同一线程中
            Users users = userCheckClient.check(username);

            if(users==null){
                String md5Pwd = MD5Utils.md5(password);
                Users user = new Users();
                user.setUsername(username);
                user.setUserImg("img/default.png");
                user.setPassword(md5Pwd);
                user.setUserModtime(new Date());
                user.setUserRegtime(new Date());
                int i = userSaveClient.createUser(user);

                if(i>0){
                    return  new ResultVo(ResultStatus.OK,"注册成功",user);
                }else{
                    return  new ResultVo(ResultStatus.NO,"注册失败",null);
                }
            }else if(users.getUsername() == null){
                return  new ResultVo(ResultStatus.NO,"网络出现了故障，清重试",null);
            }else{
                return  new ResultVo(ResultStatus.NO,"该用户已注册",null);
            }
        }

    }
}
